home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.lang.c
- Path: netcom.com!smryan
- From: smryan@netcom.com (@#$%!?!)
- Subject: Re: [Q] Best handling of non-fixed-length data??
- Message-ID: <smryanDq6Jys.GnD@netcom.com>
- Organization: The Programmer formerly known as S M Ryan
- X-Newsreader: TIN [version 1.2 PL1]
- References: <4l629f$4cc@nuke.csu.net>
- Date: Sat, 20 Apr 1996 21:35:16 GMT
- Sender: smryan@netcom4.netcom.com
-
- : Hi there, and thanks in advance for taking the time.
- : I consider myself an adept, however novice, programmer in C;
- : but I need advice on the best way to manage non-fixed-length data.
-
- Externally, you can use various organisations like
- <question number><answer text>\n
- ...
- with repeatable question numbers or
- <question number> (<answer text>;....)\n
- with appropriate delimiters or
- <question number> <number of answers> <answer length1><answer1> ...
-
- Internally, you can have an array <questionNumber,answerText> with
- repeatable questionNumbers in the array, or <questionNumber,array of
- answerTexts>.
-
-
- If you have all the lengths precomputed and read in with the data, you
- can allocate all of the various arrays and strings as you see them and
- then just fill them in.
-
- If you only know the lengths once you've read the entire value, you can
- read once for length, backup, and reread for content. Or you can adjust
- the sizes of arrays as you go along, or create a linked list.
-
- Letting T be some type, then you can add one more element to an array
- of T with something like:
-
- T *arr=0; int arrlen=0,arrx=0;
-
- T *addOneMoreT(T *array,int *length,int *actual,T value) {
- /* Initialise. */
- if (*atual==0) {*actual = 1; array = malloc(sizeof(T));}
-
- /* Ensure array is long enough for another. */
- if (*length>=*actual) {
- *actual += *actual;
- array = realloc(array,*actual * sizeof(T));
- }
-
- /* Add the element. */
- array[*length] = value;
- *length += 1;
- }
-
- Keep calling arr=addOneMoreT(arr,&arrlen,&arrx,val) for each question
- answer pair, or each answer of an array, or each character of a string.
- Once you've established the maximum size, you can clean up with
- arr = realloc(arr,arrlen*sizeof(T));
- And then you iterate through the array with
- int i;
- for (i=0; i<arrlen; i++)
- ...arr[i]...
-
- or T *elem;
- for (elem=arr; elem<arr+arrlen; elem++)
- ...*elem...
-
-
- You can also use linked lists
-
- typedef struct T tT,*pT;
- struct T {
- pT link;
- D data;
- };
-
- pT addOneMoreT(pT link,D value) {
- pT result = malloc(sizeof(tT));
- result->link = link;
- result->data = value;
- return result;
- }
-
- pT arr=0;
-
- Keeping calling arr=addOneMoreT(arr,value) for element. And then you iterate
- through the array with
- pT elem;
- for (elem=arr; elem; elem=elem->link)
- ...elem->data...
- --
- The Queen who loves, the Queen of life, | smryan@netcom.com PO Box 1563
- the Queen who straits, the Queen of strife;| Cupertino, California
- with gasp of death or gift of breath | (xxx)xxx-xxxx 95015
- she brings the choice of birth or knife. | I don't use no smileys
-